package com.zoe.vhr.config;

import cn.hutool.extra.spring.SpringUtil;
import com.zoe.vhr.enums.MailConfigEnum;
import com.zoe.vhr.service.MailSendLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.util.concurrent.FailureCallback;
import org.springframework.util.concurrent.SuccessCallback;

/**
 * @author zoe.Zhang
 * @date 2023/01/05
 * @description
 */
@Slf4j
public class RabbitCallBack {

    public static SuccessCallback<CorrelationData.Confirm> getConfirmSuccessCallback(String msgId,
                                                                                     CorrelationData correlationData) {
        MailSendLogService mailSendLogService = SpringUtil.getBean(MailSendLogService.class);
        return (result) -> {
            if (result.isAck()) {
                log.info("\n 消息投递到交换机成功===>{} \n", correlationData.getId());
                mailSendLogService.updateSendLog(msgId, MailConfigEnum.SUCCESS);
            }
            else {
                log.info("\n 消息投递到交换机失败===>{} \n", correlationData.getId());
                mailSendLogService.updateSendLog(msgId, MailConfigEnum.SENDING);
            }
        };
    }

    public static FailureCallback getFailureCallback(String msgId, CorrelationData correlationData) {
        MailSendLogService mailSendLogService = SpringUtil.getBean(MailSendLogService.class);
        return (exception) -> {
            mailSendLogService.updateSendLog(msgId, MailConfigEnum.SENDING);
            log.info("\n 消息投递到交换机过程异常===>{} \n", correlationData.getId());
        };
    }

}
